﻿using System.Collections.Generic;
using Microsoft.Xna.Framework;

namespace IcicleFramework.Actions
{
    public class ParallelGameAction : CompositeGameAction
    {
        protected List<IGameAction> toRemove;

        public ParallelGameAction()
        {
            toRemove = new List<IGameAction>();
        }

        protected ParallelGameAction(ParallelGameAction old)
            : base(old)
        {
            toRemove = new List<IGameAction>();
        }

        public override void Update(GameTime gameTime)
        {
            toRemove.Clear();

            foreach (var action in actions)
            {
                action.Update(gameTime);

                if (action.Finished)
                {
                    toRemove.Add(action);
                }
            }

            foreach (var action in toRemove)
            {
                actions.Remove(action);
            }
        }

        public override IGameAction DeepClone()
        {
            return new ParallelGameAction(this);
        }
    }
}
